#include "apue.h"
#include <dirent.h>
#include <errno.h>

void list_dir_files(DIR *);

int main(int argc, char *argv[]) {
    int fd;
    DIR *d;
    if (argc != 2)
        err_quit("Usage %s fd\n", argv[0]);

    fd= strtol(argv[1], NULL, 10);
    if ((d= fdopendir(fd)) == NULL)
        err_sys("cant open fd %d", fd);

    list_dir_files(d);
}

void list_dir_files(DIR * dir) {
    struct dirent * ent;
    rewinddir(dir);

    errno=0;
    while ((ent= readdir(dir)) != NULL) {
        if (errno != 0)
            err_sys("readdir error");

        if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0)
            continue;

        printf("%ld %s\n",ent->d_ino, ent->d_name);
    } 
}
